package com.kefu.chat.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.chat.model.ChatReply;
import com.kefu.chat.service.ChatReplyService;
import com.kefu.chat.vo.ChatReplyInsertVo;
import com.kefu.chat.vo.ChatReplySearchVo;
import com.kefu.chat.vo.ChatReplyUpdateVo;
import com.kefu.common.annotation.Permission;
import com.kefu.common.context.KefuContext;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.BatchVo;
import com.kefu.common.vo.DeleteVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

@Api(tags="快捷回复")
@RestController
@RequestMapping("chat/reply")
public class ChatReplyController extends AbstractBaseController{
    @Resource
    private ChatReplyService chatReplyService;

    @PostMapping("/add")
    @ApiOperation(value="新增快捷回复", notes="新增快捷回复")
    public Result<String> add(@Validated @RequestBody ChatReplyInsertVo param) {
    	logger.info("新增快捷回复:{}",param);
    	ChatReply info = param.convert();
    	info.setWeight(chatReplyService.getMaxWeight(KefuContext.getKefuContext().getTenantId(),param.getParentId())+1);
        chatReplyService.save(info);
        return ResultGenerator.genSuccessResult(info.getId());
    }

    @PostMapping("/importReply")
    @ApiOperation(value = "导入快捷回复")
    public Result<String> importReply(MultipartFile file) throws IOException {
        logger.info("导入快捷回复");
        return ResultGenerator.genSuccessResult(chatReplyService.importReply(file.getInputStream()));
    }


    @PostMapping("/delete")
    @ApiOperation(value="删除快捷回复", notes="删除快捷回复")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
    	ChatReply reply = chatReplyService.findById(delete.getId());
        logger.info("删除快捷回复:{},{}",delete,reply);
        chatReplyService.deleteById(delete.getId());
        chatReplyService.deleteByParentId(reply.getTenantId(),reply.getId());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value="删除快捷回复", notes="删除快捷回复")
    public Result<String> deleteBatch(@Validated @RequestBody BatchVo delete) {
        logger.info("删除快捷回复:{}",delete);
        chatReplyService.deleteByIds(delete.getIds());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ApiOperation(value="修改快捷回复", notes="修改快捷回复")
    public Result<String> update(@Validated @RequestBody ChatReplyUpdateVo chatReplyVo) {
    	logger.info("修改快捷回复:{}",chatReplyVo);
    	ChatReply info= chatReplyVo.convert();
        chatReplyService.update(info);
        return ResultGenerator.genSuccessResult();
    }


    @PostMapping("/list")
    @ApiOperation(value="查询快捷回复", notes="查询快捷回复")
    public Result<Page<ChatReply>> list(@Validated @RequestBody ChatReplySearchVo search) {    
        Page<ChatReply> list = chatReplyService.searchLambdaPage(search);
        return ResultGenerator.genSuccessResult(list);
    }
}
